/*
 * Copyright 2002-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hqd.ch03.v44.web.servlet;


import com.hqd.ch03.utils.LinkedMultiValueMap;
import com.hqd.ch03.utils.MultiValueMap;
import com.hqd.ch03.utils.ObjectUtils;
import com.hqd.ch03.v44.utils.StringUtils;

import java.util.HashMap;

/**
 * 闪存
 */
public final class FlashMap extends HashMap<String, Object> implements Comparable<FlashMap> {

    private final MultiValueMap<String, String> targetRequestParams = new LinkedMultiValueMap<>(3);
    /**
     * 目标请求路径
     */
    private String targetRequestPath;
    /**
     * 过期时间
     */
    private long expirationTime = -1;

    public void startExpirationPeriod(int timeToLive) {
        this.expirationTime = System.currentTimeMillis() + timeToLive * 1000;
    }

    public String getTargetRequestPath() {
        return this.targetRequestPath;
    }

    public void setTargetRequestPath(String path) {
        this.targetRequestPath = path;
    }

    public FlashMap addTargetRequestParams(MultiValueMap<String, String> params) {
        if (params != null) {
            params.forEach((key, values) -> {
                for (String value : values) {
                    addTargetRequestParam(key, value);
                }
            });
        }
        return this;
    }

    public FlashMap addTargetRequestParam(String name, String value) {
        if (StringUtils.hasText(name) && StringUtils.hasText(value)) {
            this.targetRequestParams.add(name, value);
        }
        return this;
    }

    public MultiValueMap<String, String> getTargetRequestParams() {
        return this.targetRequestParams;
    }


    public long getExpirationTime() {
        return this.expirationTime;
    }

    public void setExpirationTime(long expirationTime) {
        this.expirationTime = expirationTime;
    }

    public boolean isExpired() {
        return (this.expirationTime != -1 && System.currentTimeMillis() > this.expirationTime);
    }


    @Override
    public int compareTo(FlashMap other) {
        /**
         * targetRequestPath 为空的优先
         */
        int thisUrlPath = (this.targetRequestPath != null ? 1 : 0);
        int otherUrlPath = (other.targetRequestPath != null ? 1 : 0);
        if (thisUrlPath != otherUrlPath) {
            return otherUrlPath - thisUrlPath;
        } else {
            /**
             * 参数多的优先
             */
            return other.targetRequestParams.size() - this.targetRequestParams.size();
        }
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof FlashMap)) {
            return false;
        }
        FlashMap otherFlashMap = (FlashMap) other;
        return (super.equals(otherFlashMap) &&
                ObjectUtils.nullSafeEquals(this.targetRequestPath, otherFlashMap.targetRequestPath) &&
                this.targetRequestParams.equals(otherFlashMap.targetRequestParams));
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + ObjectUtils.nullSafeHashCode(this.targetRequestPath);
        result = 31 * result + this.targetRequestParams.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "FlashMap [attributes=" + super.toString() + ", targetRequestPath=" +
                this.targetRequestPath + ", targetRequestParams=" + this.targetRequestParams + "]";
    }

}
